<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="父级行政" prop="parentId">
      <el-input v-model="dataForm.parentId" placeholder="父级行政"></el-input>
    </el-form-item>
    <el-form-item label="行政名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="行政名称"></el-input>
    </el-form-item>
    <el-form-item label="" prop="areaCode">
      <el-input v-model="dataForm.areaCode" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="cityCode">
      <el-input v-model="dataForm.cityCode" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="mergerName">
      <el-input v-model="dataForm.mergerName" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="shortName">
      <el-input v-model="dataForm.shortName" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="zipCode">
      <el-input v-model="dataForm.zipCode" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="行政区域等级（0: 省级 1:市级 2:县级 3:镇级 4:乡村级）" prop="level">
      <el-input v-model="dataForm.level" placeholder="行政区域等级（0: 省级 1:市级 2:县级 3:镇级 4:乡村级）"></el-input>
    </el-form-item>
    <el-form-item label="" prop="lng">
      <el-input v-model="dataForm.lng" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="lat">
      <el-input v-model="dataForm.lat" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="拼音" prop="pinyin">
      <el-input v-model="dataForm.pinyin" placeholder="拼音"></el-input>
    </el-form-item>
    <el-form-item label="首字母" prop="first">
      <el-input v-model="dataForm.first" placeholder="首字母"></el-input>
    </el-form-item>
    <el-form-item label="" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="updateUser">
      <el-input v-model="dataForm.updateUser" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="createUser">
      <el-input v-model="dataForm.createUser" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          parentId: '',
          name: '',
          areaCode: '',
          cityCode: '',
          mergerName: '',
          shortName: '',
          zipCode: '',
          level: '',
          lng: '',
          lat: '',
          pinyin: '',
          first: '',
          updateTime: '',
          updateUser: '',
          createTime: '',
          createUser: ''
        },
        dataRule: {
          parentId: [
            { required: true, message: '父级行政不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '行政名称不能为空', trigger: 'blur' }
          ],
          areaCode: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          cityCode: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          mergerName: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          shortName: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          zipCode: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          level: [
            { required: true, message: '行政区域等级（0: 省级 1:市级 2:县级 3:镇级 4:乡村级）不能为空', trigger: 'blur' }
          ],
          lng: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          lat: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          pinyin: [
            { required: true, message: '拼音不能为空', trigger: 'blur' }
          ],
          first: [
            { required: true, message: '首字母不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          updateUser: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          createUser: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/aggregation/area/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.parentId = data.area.parentId
                this.dataForm.name = data.area.name
                this.dataForm.areaCode = data.area.areaCode
                this.dataForm.cityCode = data.area.cityCode
                this.dataForm.mergerName = data.area.mergerName
                this.dataForm.shortName = data.area.shortName
                this.dataForm.zipCode = data.area.zipCode
                this.dataForm.level = data.area.level
                this.dataForm.lng = data.area.lng
                this.dataForm.lat = data.area.lat
                this.dataForm.pinyin = data.area.pinyin
                this.dataForm.first = data.area.first
                this.dataForm.updateTime = data.area.updateTime
                this.dataForm.updateUser = data.area.updateUser
                this.dataForm.createTime = data.area.createTime
                this.dataForm.createUser = data.area.createUser
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/aggregation/area/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'parentId': this.dataForm.parentId,
                'name': this.dataForm.name,
                'areaCode': this.dataForm.areaCode,
                'cityCode': this.dataForm.cityCode,
                'mergerName': this.dataForm.mergerName,
                'shortName': this.dataForm.shortName,
                'zipCode': this.dataForm.zipCode,
                'level': this.dataForm.level,
                'lng': this.dataForm.lng,
                'lat': this.dataForm.lat,
                'pinyin': this.dataForm.pinyin,
                'first': this.dataForm.first,
                'updateTime': this.dataForm.updateTime,
                'updateUser': this.dataForm.updateUser,
                'createTime': this.dataForm.createTime,
                'createUser': this.dataForm.createUser
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
